﻿using System.Linq;
using System.Windows;
using System.Windows.Media;
using System;

namespace ZOIL.Framework.Presentation
{
    /// <summary>
    /// Zoom Modifier for render and layout transformations.
    /// </summary>
    public class ScaleModifier : ZoomModifier
    {
        #region dp

        /// <summary>
        /// 
        /// </summary>
        public static readonly DependencyProperty WidthModifierProperty = DependencyProperty.Register("WidthModifier", typeof(double), typeof(ScaleModifier), new PropertyMetadata(0.0));

        /// <summary>
        /// 
        /// </summary>
        public double WidthModifier
        {
            get { return (double)this.GetValue(WidthModifierProperty); }
            set { this.SetValue(WidthModifierProperty, value); }
        }

        /// <summary>
        /// 
        /// </summary>
        public static readonly DependencyProperty HeightModifierProperty = DependencyProperty.Register("HeightModifier", typeof(double), typeof(ScaleModifier), new PropertyMetadata(0.0));

        /// <summary>
        /// 
        /// </summary>
        public double HeightModifier
        {
            get { return (double)this.GetValue(HeightModifierProperty); }
            set { this.SetValue(HeightModifierProperty, value); }
        }

        #endregion

        #region overrides

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="zoom"></param>
        public override void Update(DependencyObject source, double zoom)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            base.Update(source, zoom);
          
#if DEBUG
            if (this.Property.PropertyType.BaseType != typeof(GeneralTransform))
                throw new InvalidOperationException("ScaleModifier must target either RenderTransform or LayoutTransform.");
#endif
            var scaleTransform = new ScaleTransform(Interpolate(zoom, this.WidthModifier), Interpolate(zoom, this.HeightModifier));
            source.SetValue(this.Property, scaleTransform);
        }
        
        #endregion
    }
}
